import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BlogDao {
    public void insert(Blog blog){
        PreparedStatement statement=null;
        Connection connection=null;
        try {
            String sql="insert into blog values (null,?,?,now(),?)";
            connection=DBUtil.getConnection();
            statement=connection.prepareStatement(sql);
            statement.setString(1,blog.getTitle());
            statement.setString(2,blog.getContent());
            statement.setInt(3,blog.getUserId());

            int ret=statement.executeUpdate();
            if(ret==1){
                System.out.println("博客插入成功");
                //更新数据库
                UserDao userDao=new UserDao();
                userDao.upDate(1);
            }else {
                System.out.println("插入失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(null,statement,connection);
        }
    }

    public Blog selectById(int blogId){

        Connection connection=null;
        PreparedStatement statement = null;
        ResultSet resultSet=null;

        try {
            String sql="select * from blog where blogId = ?";
            connection=DBUtil.getConnection();
            statement=connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            resultSet=statement.executeQuery();
            if(resultSet.next()){
                Blog blog =new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                return blog;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(resultSet,statement,connection);
        }
        return null;
    }
    public List<Blog> selectAll(){
        List<Blog>list=new ArrayList<>();
        Connection connection=null;
        PreparedStatement statement = null;
        ResultSet resultSet=null;

        try {
            String sql="select * from blog order by postTime desc ";
            connection=DBUtil.getConnection();
            statement=connection.prepareStatement(sql);
            resultSet=statement.executeQuery();
            while (resultSet.next()){
                Blog blog=new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                list.add(blog);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(resultSet,statement,connection);
        }
        return list;
    }

    public void deleteById(int blogId){
        Connection connection=null;
        PreparedStatement statement=null;

        try {
            String sql="delete from blog where blogId=?";
            connection=DBUtil.getConnection();
            statement=connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            int ret = statement.executeUpdate();
            if(ret==1){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(null,statement,connection);
        }
    }
}
